package com.shop.usercenter.service;

import com.shop.usercenter.entity.User;
import com.shop.usercenter.repository.UserRepository;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * user suervice
 *
 * @author chenye
 * @data 2020-0309
 */
@Service
public class UserService {

    @Resource
    UserRepository userRepository;

    public List<User> getUserList() {
        return userRepository.findAll();
    }

    public ResponseEntity login(String name, String password) {
        User user = userRepository.findUserByName(name);
        if (user == null) {
            return ResponseEntity.badRequest().body("用户不存在");
        }
        if (password.equals(user.getPassword())) {
            return ResponseEntity.badRequest().body("密码错误");
        }
        return ResponseEntity.ok().body("登录成功");
    }

    public User addUser(User user) {
        return userRepository.save(user);
    }
}
